package com.farsunset.ichat.cim.handler;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handler.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.ichat.common.util.ContextHolder;
import com.kuaigou.cim.model.TrackBo;
import com.kuaigou.cim.service.TruckerTrailSendService;
import com.kuaigou.cim.util.JSONHelper;
import com.kuaigou.common.model.bean.Track;

/**
 * 司机上报轨迹
 *
 */
public class TruckerTrailReportHandler implements CIMRequestHandler {

protected final Logger logger = Logger.getLogger(TruckerTrailReportHandler.class);
	
	public ReplyBody process(CIMSession newSession, SentBody message) {	
		TruckerTrailSendService truckerTrailSendService = (TruckerTrailSendService)ContextHolder.getBean("truckerTrailSendService");
		ReplyBody reply = new ReplyBody();
		try { 						
			reply.setKey(CIMConstant.RequestKey.CLIENT_TRUCKER_TRAIL_REPORT);
			String trail = message.get("trail");
			List<TrackBo> listbo = JSONHelper.jsonToList(trail, TrackBo.class);
			List<Track> list = new ArrayList<Track>();
			for(TrackBo bo : listbo){
				list.add(convertTo(bo));
			}			
			truckerTrailSendService.send(list);
			reply.setCode(CIMConstant.ReturnCode.CODE_200);
		} catch (Exception e) {
			reply.setCode(CIMConstant.ReturnCode.CODE_500);
			e.printStackTrace();
		}
		return reply;
	}	
	
	private Track convertTo(TrackBo trackbo){
		    Track track = new Track(); 
		    track.setAccountId(trackbo.getAccount());
	        track.setCityCode(trackbo.getCityCode());
	        track.setCityId(trackbo.getCityId());
	        track.setDirection(trackbo.getDirection());
	        track.setAltitude(trackbo.getAltitude());
	        track.setGpsTime(trackbo.getGpsTime());
	        track.setAddress(trackbo.getAddress());
	        track.setLatitude(trackbo.getLatitude());
	        track.setLongitude(trackbo.getLongitude());
	        track.setSpeed(trackbo.getSpeed());
	        return track;
	}


}
